/**
 * 
 */
package com.nari.interfaces.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.interfaces.model.IUploadMissionState;
import com.nari.platform.date.CalendarUtils;

/**
 * 
 * @author Xiaopeng
 * @date 2013-11-11
 */
public class UploadMissionStateDaoImpl extends GenericDaoImpl<IUploadMissionState, String> 
		implements IUploadMissionStateDao {

	/**
	 * 通过任务标志(集中器号、抄表日期)查询任务实体
	 * @param concentratorNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-11
	 */
	public IUploadMissionState queryMissionByMark(String concentratorNo, 
			String recordTime) throws Exception {
		String hsql ="from IUploadMissionState ums " +
				"where lower(ums.concentratorNo)='"+concentratorNo.toLowerCase()+"' " +
				"and ums.recordTime='"+recordTime+"'";
		List<IUploadMissionState> uploadMissions = this.runQueryHSql(hsql);
		IUploadMissionState mission = null;
		if(uploadMissions!=null && uploadMissions.size()>0) {
			mission = uploadMissions.get(0);
		}
		return mission;
	}
	
	/**
	 * 通过任务标志(集中器号、抄表日期)删除任务实体
	 * @param concentratorNo
	 * @param recordTime
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-11
	 */
	public void deleteMissionByMark(String concentratorNo, String recordTime) throws Exception {
		String hsql ="delete from IUploadMissionState ums " +
				"where lower(ums.concentratorNo)='"+concentratorNo.toLowerCase()+"' " +
				"and ums.recordTime='"+recordTime+"'";
		this.runFormatHSql(hsql);
	}
	
	/**
	 * 查询超时指定小时数（相对当前时刻）的上传任务数据
	 * @param hourAmount 用于设置的小时数
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-25
	 */
	@SuppressWarnings("unchecked")
	public List<IUploadMissionState> queryExceedHoursMission(int hourAmount) throws Exception {
		//查询缓存时间超过3h的任务数据
//		String hsql = "from IUploadMissionState ums " +
//				"where (year(CURRENT_DATE())*365*24 " +
//						"+ month(CURRENT_DATE())*30*24 " +
//						"+ day(CURRENT_DATE())*24 " +
//						"+ hour(CURRENT_TIME()) + 8 " +
//						"+ minute(CURRENT_TIME())/60) " +
//					"- (year(ums.createTime)*365*24 " +
//						"+ month(ums.createTime)*30*24 " +
//						"+ day(ums.createTime)*24 " +
//						"+ hour(ums.createTime) " +
//						"+ minute(ums.createTime)/60) >=3";
		Date current = new Date();
		Date exceedDate = CalendarUtils.addHours(current, hourAmount);
		/* 超时处理公式：createTime+exceedHour<=currentTime
		 * 公式处理：createTime<=currentTime-exceedHour */
		String hsql = "from IUploadMissionState ums " +
				"where ums.createTime<=:exceedDate";
		Query query = this.getCurrentSession().createQuery(hsql);
		query.setTimestamp("exceedDate", exceedDate);
		return query.list();
	}
}
